﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Media.Imaging;
using System.Windows;
using System.Windows.Media;
using System.Windows.Controls;

namespace Shrestha.Charts.Graph
{
    class GraphUtils
    {
        [DllImport("gdi32")]
        static extern int DeleteObject(IntPtr o);

        public static BitmapSource loadBitmap(System.Drawing.Bitmap source)
        {
            IntPtr ip = source.GetHbitmap();
            BitmapSource bs = null;
            try
            {
                bs = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(ip,
                   IntPtr.Zero, Int32Rect.Empty,
                   System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
            }
            finally
            {
                DeleteObject(ip);
            }

            return bs;
        }

        public static void SaveWindow(Window window, int dpi, string filename) 
        {

            var rtb = new RenderTargetBitmap( 
                (int)window.Width, //width 
                (int)window.Height, //height 
                dpi, //dpi x 
                dpi, //dpi y 
                PixelFormats.Pbgra32 // pixelformat 
                ); 
            rtb.Render(window);

            SaveRTBAsPNG(rtb, filename);

        }

        public static void SaveCanvas(Window window, Canvas canvas, int dpi, string filename) 
        { 
            Size size = new Size(window.Width , window.Height ); 
            canvas.Measure(size); 
            //canvas.Arrange(new Rect(size));

            var rtb = new RenderTargetBitmap( 
                (int)window.Width, //width 
                (int)window.Height, //height 
                dpi, //dpi x 
                dpi, //dpi y 
                PixelFormats.Pbgra32 // pixelformat 
                ); 
            rtb.Render(canvas);

            SaveRTBAsPNG(rtb, filename); 
        }

        private static void SaveRTBAsPNG(RenderTargetBitmap bmp, string filename) 
        {
            var enc = new System.Windows.Media.Imaging.PngBitmapEncoder();
            enc.Interlace = PngInterlaceOption.On;
            enc.Frames.Add(System.Windows.Media.Imaging.BitmapFrame.Create(bmp));

            using (var stm = System.IO.File.Create(filename)) 
            { 
                enc.Save(stm); 
            } 
        } 
    } 
}
